﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using HotelManagement.Class;
using System.Data;

namespace HotelManagement
{
    public partial class CreateHousekeepingSchedule : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                getGeneralMaintenanceStaff();
                getEstateMaintenanceStaff();
                getRoomMaintenanceStaff();
                getSecurityMaintenanceStaff();
            }
        }

        protected void btnCreateHousekeeping_Click(object sender, EventArgs e)
        {
            createHousekeepingSchedule();
        }
        protected void btnAddNewStaff_Click(object sender, EventArgs e)
        {
            try
            {
                //createHousekeepingSchedule();
                displayData();
                tbxDescription1.Text = String.Empty;
                tbxScheduleID.Text = String.Empty;

                lblStatus.Text = "Schedule for "+ tbxYear.Text + "-" + ddlMonth.Text + "-" + tbxDay.Text;;
            }
            catch (Exception)
            {
                lblStatus.Text = "Please Select Date to Continue";
            }
        }

        protected void btnGoBack_Click(object sender, EventArgs e)
        {
            Server.Transfer("ManageHousekeepingAndStaff.aspx");
        }

        private void createHousekeepingSchedule()
        {
            try
            {

                //Get Date
                DateTime date = (DateTime)Convert.ToDateTime(tbxDay.Text + "/" + ddlMonth.SelectedValue + "/" + tbxYear.Text);

                //Create HousekeepingSchedule
                //Display Create success or fail
                //Condition if Date is valid

                bool result = isDateValid();

                if (result == true)
                {

                    HousekeepingSchedule s = new HousekeepingSchedule(tbxScheduleID.Text, date, ddlGmS1.SelectedValue, ddlRmS1.SelectedValue, ddlEmS1.SelectedValue, ddlSmS1.SelectedValue, tbxDescription1.Text);
                    if (HousekeepingDBManager.insertHousekeepingSchedule(s) == 1)
                    {
                        lblStatus.Text = "Create Housekeeping schedule successful";
                    }
                    else
                    {
                        lblStatus.Text = "Create Housekeeping schedule failed. Please try again";
                    }


                }
                else
                {
                    lblStatus.Text = "Entered Date is invalid. Please try again";
                }

            }
            catch (Exception)
            {
                lblStatus.Text = "The Schedule ID you are trying to create is already in the Database. Check the Schedule ID.";
            }
        }
   
        private void getSecurityMaintenanceStaff()
        {

            ddlSmS1.Items.Clear();
            string selectSQL = "SELECT FirstName, LastName, StaffID FROM Staff WHERE DutyType='Security Maintenance'";

            SqlDataReader reader;
            ListItem newItem = new ListItem();

            try
            {
                SqlConnection con = StaffDBManager.ConnectToDatabase();

                SqlCommand cmd = new SqlCommand(selectSQL, con);

                con.Open();
                reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    newItem = new ListItem();
                    newItem.Text = reader["StaffID"].ToString() + " - " + reader["FirstName"].ToString() + " " + reader["LastName"].ToString();
                    newItem.Value = reader["StaffID"].ToString() + " - " + reader["FirstName"].ToString() + " " + reader["LastName"].ToString();
                    ddlSmS1.Items.Add(newItem);
                }
                reader.Close();

            }
            catch (SqlException ex1)
            {
                lblStatus.Text = "Error: " + ex1 + "Please try again";
            }
        }

        private void getRoomMaintenanceStaff()
        {
            ddlRmS1.Items.Clear();
            string SelectSQL = "SELECT FirstName,LastName, StaffID FROM Staff WHERE DutyType='Room Maintenance'";
            SqlDataReader reader;

            try
            {
                SqlConnection con = StaffDBManager.ConnectToDatabase();
                SqlCommand cmd = new SqlCommand(SelectSQL, con);
                ListItem newItem = new ListItem();
           

                con.Open();
                reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    newItem = new ListItem();
                    newItem.Text = reader["StaffID"].ToString() + " - " + reader["FirstName"].ToString() + " " + reader["LastName"].ToString();
                    newItem.Value = reader["StaffID"].ToString() + " - " + reader["FirstName"].ToString() + " " + reader["LastName"].ToString();
                    ddlRmS1.Items.Add(newItem);
                }
                reader.Close();

            }
            catch (SqlException ex1)
            {
                lblStatus.Text = "Error: " + ex1 + "Please try again";
            }
        }

        private void getEstateMaintenanceStaff()
        {
            ddlEmS1.Items.Clear();
            string SelectSQL = "SELECT FirstName,LastName, StaffID FROM Staff WHERE DutyType='Estate Maintenance'";
            SqlDataReader reader;

            try
            {
                SqlConnection con = StaffDBManager.ConnectToDatabase();
                SqlCommand cmd = new SqlCommand(SelectSQL, con);
                ListItem newItem = new ListItem();
                

                con.Open();
                reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    newItem = new ListItem();
                    newItem.Text = reader["StaffID"].ToString() + " - " + reader["FirstName"].ToString() + " " + reader["LastName"].ToString();
                    newItem.Value = reader["StaffID"].ToString() + " - " + reader["FirstName"].ToString() + " " + reader["LastName"].ToString();
                    ddlEmS1.Items.Add(newItem);
                    
                }
                reader.Close();

            }
            catch (SqlException ex1)
            {
                lblStatus.Text = "Error: " + ex1 + "Please try again";
            }
        }

        private void getGeneralMaintenanceStaff()
        {
            ddlGmS1.Items.Clear();
            string selectSQL = "SELECT FirstName,LastName, StaffID FROM Staff WHERE DutyType='General Maintenance'";

            SqlDataReader reader;
            ListItem newItem = new ListItem();

            try
            {
                SqlConnection con = StaffDBManager.ConnectToDatabase();

                SqlCommand cmd = new SqlCommand(selectSQL, con);

                con.Open();
                reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    newItem = new ListItem();
                    newItem.Text = reader["StaffID"].ToString() + " - " + reader["FirstName"].ToString() + " " + reader["LastName"].ToString();
                    newItem.Value = reader["StaffID"].ToString() + " - " + reader["FirstName"].ToString() + " " + reader["LastName"].ToString();
                    ddlGmS1.Items.Add(newItem);
                }
                reader.Close();

            }
            catch (SqlException ex1)
            {
                lblStatus.Text = "Error: " + ex1 + "Please try again";
            }
        }

        private Boolean isDateValid()
        {
            DateTime today = DateTime.Today;

            int dayToday = Convert.ToInt32(today.Day);
            int monthToday = Convert.ToInt32(today.Month);
            int yearToday = Convert.ToInt32(today.Year);

            int day = Convert.ToInt32(tbxDay.Text);
            int month = Convert.ToInt32(ddlMonth.SelectedValue);
            int year = Convert.ToInt32(tbxYear.Text);

            if (year < yearToday)
            {
                return false;
            }
            else if ( month < monthToday && year==yearToday)
            {
                return false;
            }
            else if (year == yearToday && month == monthToday && day <= dayToday)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        private void displayData()
        {
            string date = tbxYear.Text + "-" + ddlMonth.SelectedValue + "-" + tbxDay.Text;
            //lblDate.Text = date;
            DataSet ds = new DataSet();
            SqlDataAdapter da = new SqlDataAdapter();
            if (date != null)
            {
                SqlConnection con = StaffDBManager.ConnectToDatabase();
                con.Open();


                da = new SqlDataAdapter("SELECT scheduleID, GeneralMaintenanceStaffID, RoomMaintenanceStaffID, EstateMaintenanceStaffID, SecurityMaintenanceStaffID FROM HousekeepingSchedule WHERE ScheduleDate='" + date + "'", con);
                da.Fill(ds);
                GridView1.DataSource = ds.Tables[0];
                GridView1.DataBind();
                con.Close();
            }
        }




    }
}